package com.yuyou.fn.timer.task;

import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.educational.entity.FinishLecture;
import com.yuyou.fn.educational.service.IFinishLectureService;
import com.yuyou.fn.portal.entity.OuterCourseOrder;
import com.yuyou.fn.portal.service.IOuterCourseOrderService;
import com.yuyou.fn.settlement.entity.PayOrder;
import com.yuyou.fn.settlement.service.IPayOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by wens on 2017/4/19.
 */
public class CalFinishOuterOrderTask implements Runnable {


    private static final Logger log = LoggerFactory.getLogger(CalFinishOuterOrderTask.class);

    @Resource
    private IFinishLectureService finishLectureService ;

    @Resource
    private IOuterCourseOrderService outerCourseOrderService;

    @Resource
    private IPayOrderService payOrderService;

    @Override
    public void run() {
        log.info("开始检测完成未完成的订单");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dateNow = new Date();
        Calendar cl = Calendar.getInstance();
        cl.setTime(dateNow);
//		cl.add(Calendar.DAY_OF_YEAR, -1);	//一天
    	cl.add(Calendar.WEEK_OF_YEAR, -1);	//一周
//		cl.add(Calendar.MONTH, -1);			//一个月
        Date dateFrom = cl.getTime();

        List<OuterCourseOrder> outerCourseOrderList=outerCourseOrderService.findOuterCourseOrder(0,dateFrom);
        for(OuterCourseOrder outerCourseOrder:outerCourseOrderList){
            Long outerCourseOrderId=outerCourseOrder.getOuterCourseOrderId();
            String channel="wx_pub";
            PayOrder payOrder=payOrderService.findByPayOrderIdAndChannel(outerCourseOrderId,channel);
            if(payOrder!=null){
                Integer status=payOrder.getStatus();
                BigDecimal  amount=payOrder.getAmount();
                if(status==1){
                    outerCourseOrderService.payOuterCourseOrder(outerCourseOrderId,amount,4,null);
                }
            }

        }
        log.info("检测完成订单结束");
    }

}
